﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Avatar.Company;
using Avatar.Common;
using Avatar.MembershipService;

public partial class Company_Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Company"] != null)
            {
                var _company = Request.QueryString["Company"].ToString();

                var _information = CompanyInformationManager.GetCompanyInformation(_company);

                if (_information == null) Helper.ShowMassage("您的企业基本资料尚未填写，接下来请填写您企业的基本资料。", "Company_Information.aspx");

                var _c = CompanyContactManager.GetContact(_company);
                if (_c != null)
                {
                    PanelView.Visible = true;
                    PanelEdit.Visible = false;

                    lbl_Address.Text = _c.Address;
                    lbl_Contact.Text = _c.Contact;
                    lbl_Email.Text = _c.Email;
                    lbl_Tel.Text = _c.Tel;
                    lbl_WebSite.Text = _c.WebSite;
                    lbl_WebSite.NavigateUrl = _c.WebSite;
                }
                else
                {
                    PanelView.Visible = false;
                    PanelEdit.Visible = true;
                }
            }
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();

            CompanyContactManager.SaveContact(_company,
                txt_Address.Text,
                txt_Contact.Text,
                txt_Email.Text,
                txt_Tel.Text,
                txt_WebSite.Text);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        PanelEdit.Visible = false;
        PanelView.Visible = true;
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();

            CompanyContactManager.Delete(_company);
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        PanelEdit.Visible = true;
        PanelView.Visible = false;

        try
        {
            if (Request.QueryString["Company"] == null) throw new Exception("企业名称不能为空");
            var _company = Request.QueryString["Company"].ToString();

            var _c = CompanyContactManager.GetContact(_company);
            if (_c != null)
            {
                txt_Address.Text = _c.Address;
                txt_Contact.Text = _c.Contact;
                txt_Email.Text = _c.Email;
                txt_Tel.Text = _c.Tel;
                txt_WebSite.Text = _c.WebSite;
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
